package com.barray.log_monitor;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.FileProvider;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class LogDetailActivity extends AppCompatActivity {
    public static final String LOG_BEAN = "LOG_BEAN";
    private LogBean mLogBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_detail);
        mLogBean = (LogBean) getIntent().getSerializableExtra(LOG_BEAN);
        if(mLogBean == null) return;
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(mLogBean.getFileName());
        setSupportActionBar(toolbar);
        //添加默认的返回图标
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //设置返回键可用
        getSupportActionBar().setHomeButtonEnabled(true);
        TextView tvContent = findViewById(R.id.tv_content);
        // 读取文件并显示
        new Thread(){
            @Override
            public void run() {
                super.run();
                StringBuilder content = new StringBuilder();
                File file = new File(mLogBean.getFilePath());
                try{
                    BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                    String temp;
                    while ((temp = bufferedReader.readLine())!= null){
                        content.append(temp).append("\r\n");
                    }
                    bufferedReader.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
                runOnUiThread(()->tvContent.setText(content));
            }
        }.start();
    }


    public static void start(Context context,LogBean logBean){
        Intent intent = new Intent(context,LogDetailActivity.class);
        intent.putExtra(LOG_BEAN,logBean);
        context.startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_log_detail, menu);
        return super.onCreateOptionsMenu(menu);
    }

    public void shareFile(LogBean logBean){
        if(logBean == null || TextUtils.isEmpty(logBean.getFilePath())) return;
        Uri imageUri = file2Uri(new File(logBean.getFilePath()));
        Intent shareIntent2 = new Intent();
        shareIntent2.setAction(Intent.ACTION_SEND);
        shareIntent2.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent2.setType("*/*");
        startActivity(Intent.createChooser(shareIntent2, "send"));
    }

    private Uri file2Uri(File file){
        if(!file.exists()) return null;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
            return Uri.fromFile(file);
        }else{
            String authority = getPackageName() + ".fileprovider";
            Uri uri= FileProvider.getUriForFile(getApplication(), authority, file);
            grantUriPermission(getPackageName(), uri,
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            return uri;
        }
    }



    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
            return true;
        }else if(item.getItemId() == R.id.item_share){
            shareFile(mLogBean);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}